import Tools from '@/utils/Tools.js';
const filter = () => {
	let WECHAT = true;
	let ALIPAY = true;
	let BALANCE = true;
	let curPayKey = 'WECHAT'
	// #ifdef H5
	if (Tools.isWechat()) {
		ALIPAY = false;
	}
	// #endif
	// #ifdef MP-WEIXIN
	ALIPAY = false
	// #endif
	ALIPAY = false
	if (!WECHAT) {
		if (!ALIPAY) {
			curPayKey = 'BALANCE'
		} else {
			curPayKey = 'ALIPAY'
		}
	}
	return {
		paymentChannel: {
			WECHAT,
			ALIPAY,
			BALANCE
		},
		curPayKey

	}
}
const pay = (data, type, successCallback, failCallback) => {
	type = Number(type)
	switch (type) {
		case 10:
			break;
		case 20:
			// #ifdef H5
			if (Tools.isWechat()) {
				wxPayInWechat(data, successCallback, failCallback)
			} else {
				wxPayInH5(data)
			}
			// #endif
			// #ifdef MP-WEIXIN
			uniMpPay(data, successCallback, failCallback)
			// #endif
			break;
		case 30:
			// #ifdef H5
			alipayInH5(data)
			// #endif
			// #ifdef APP-VUE
			alipayInApp()
			// #endif
			break;
		default:
			break;
	}
}
//微信H5支付
const wxPayInH5 = (data) => {
	location.href = data.mweb_url
}

// uni支付 小程序部分
const uniMpPay = (data, successCallback, failCallback) => {
	console.log(data)
	uni.getProvider({
		service: 'payment',
		success: (res) => {
			if (res.errMsg == 'getProvider:ok') {
				const provider = res.provider[0];
				uni.requestPayment({
					provider,
					orderInfo: JSON.stringify(data),
					timeStamp: data.timeStamp || '',
					nonceStr: data.nonceStr || '',
					package: 'prepay_id=' + (data.prepay_id || ''),
					signType: data.signType || 'MD5',
					paySign: data.paySign || '',
					success: (res) => {
						successCallback && (setTimeout(() => {
							successCallback();
						}, 0));
					},
					fail() {
						let msg = '支付失败';
						Tools.showError(msg, () => {
							failCallback && setTimeout(() => {
								failCallback(msg)
							}, 1500)
						});


					}

				})
			}
		}
	})
}

const wxPayInWechat = (data, successCallback, failCallback) => {
	WeixinJSBridge.invoke(
		'getBrandWCPayRequest', {
			...data
		},
		(res) => {
			if (res.err_msg == "get_brand_wcpay_request:ok") {
				successCallback && (setTimeout(() => {
					successCallback();
				}, 0));
				// 使用以上方式判断前端返回,微信团队郑重提示：
				//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
			} else {
				let msg = '支付失败';
				if (res.err_msg == "get_brand_wcpay_request:cancel") {
					msg = '用户主动取消支付';
				}
				Tools.showError(msg, () => {
					failCallback && setTimeout(() => {
						failCallback(msg)
					}, 1500)
				});

			}
		}
	);
}

const alipayInApp = () => {};
const alipayInH5 = (data) => {
	console.log(data)
	// return false
	// #ifdef H5
	let formStr = "<form id='alipaysubmit' name='alipaysubmit' action='" + data.action;
	formStr = formStr + "' method='POST'><input type='hidden' name='biz_content' value='" + data.biz_content;
	formStr = formStr + "'/><input type='hidden' name='app_id' value='" + data.app_id;
	formStr = formStr + "'/><input type='hidden' name='version' value='" + data.version;
	formStr = formStr + "'/><input type='hidden' name='format' value='" + data.format;
	formStr = formStr + "'/><input type='hidden' name='sign_type' value='" + data.sign_type;
	formStr = formStr + "'/><input type='hidden' name='method' value='" + data.method;
	formStr = formStr + "'/><input type='hidden' name='timestamp' value='" + data.timestamp;
	formStr = formStr + "'/><input type='hidden' name='notify_url' value='" + data.notify_url;
	formStr = formStr + "'/><input type='hidden' name='return_url' value='" + data.return_url;
	formStr = formStr + "'/><input type='hidden' name='charset' value='" + data.charset;
	formStr = formStr + "'/><input type='hidden' name='sign' value='" + data.sign;
	formStr = formStr + "'/><input type='submit' value='ok' style='display:none;''></form>";
	let div = document.createElement("div");
	div.setAttribute("id", "alipayInH5");
	div.innerHTML = formStr; //res.data是返回的表单
	document.body.appendChild(div);
	document.forms.alipaysubmit.submit();
	document.body.removeChild(document.getElementById("alipayInH5"));
	// #endif
};
export default {
	pay,
	filter
}
